from flask import Flask, send_from_directory
from config import Config,CustomJSONProvider
from models import db
from routes import register_blueprints
from models.product import Product
from routes.admin_product_routes import admin_product_bp



# 创建 Flask 应用
app = Flask(__name__)

# 加载配置
app.config.from_object(Config)

# 初始化数据库
db.init_app(app)

# 注册蓝图
register_blueprints(app)

app.json = CustomJSONProvider(app)





@app.route('/')
def index():
    return '''
    <h1>欢迎访问产品管理系统</h1>
    <p>可用的页面:</p>
    <ul>
        <li><a href="/admin/login">管理员登录</a></li>
        <li><a href="/api/products">产品API</a></li>
        <li><a href="/api/swipers">轮播图API</a></li>
    </ul>
    '''

@app.route('/uploads/<category>/<filename>')
def uploaded_file(category,filename):
    return send_from_directory(Config.UPLOAD_FOLDER + "/" + category + "/", filename)

if __name__ == '__main__':
    # 创建数据库表
    with app.app_context():
        db.create_all()
    # 启动应用
    app.run(host='0.0.0.0',port=8000,debug=True)